" __  ____   __  _   ___     _____ __  __ ____   ____
"|  \/  \ \ / / | \ | \ \   / /_ _|  \/  |  _ \ / ___|
"| |\/| |\ V /  |  \| |\ \ / / | || |\/| | |_) | |
"| |  | | | |   | |\  | \ V /  | || |  | |  _ <| |___
"|_|  |_| |_|   |_| \_|  \_/  |___|_|  |_|_| \_\\____| 

" Author: @alonescar

"===
" === Basic configuration
" ===
syntax on
filetype indent on
set nocompatible
set showmode
set showcmd
set mouse=a
set t_Co=256
set wildmenu
set nobackup
set autochdir
set noerrorbells
set history=1000
set autoread
set noswapfile

" ===
" === Indent configuration
" ===
set autoindent
set tabstop=4
set softtabstop=4
set noexpandtab
set shiftwidth=4

" ===
" === Appearance settings
" ===
set number
set cursorline
set wrap
"set linebreak
set wrapmargin=2
set laststatus=2
set ruler

" ===
" === Search configuration
" ===
set hlsearch
set incsearch
set ignorecase
set smartcase
set showmatch

" ===
" === ShortCutKey configuration
" ===
map <F5> :call CompileRunGcc()<CR>

func! CompileRunGcc()
    exec "w" 
    if &filetype == 'c' 
        exec '!gcc % -o %<'
        exec '!./%<'
    elseif &filetype == 'cpp'
        exec '!g++ % -o %<'
        exec '!./%<'
    elseif &filetype == 'python'
        exec '!python3 %'
    endif                                                                              
endfunc 

noremap W :w<CR>
noremap ,q :q<CR>
noremap ,r :source $MYVIMRC<CR>

noremap J 5j
noremap K 5k
map H ^
map L $

noremap <space>h <c-w>h
noremap <space>j <c-w>j
noremap <space>k <c-w>k
noremap <space>l <c-w>l

map <up> :res +5<CR>
map <down> :res -5<CR>
map <left> :vertical resize -5<CR>
map <right> :vertical resize +5<CR>

" ===
" === Plugin configuration
" ===
call plug#begin('~/.local/share/nvim/site/plugged')
Plug 'itchyny/vim-cursorword'
"Plug 'iamcco/markdown-preview.nvim'
Plug 'jiangmiao/auto-pairs'
Plug 'Yggdroot/indentLine'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'preservim/nerdtree'
Plug 'mhinz/vim-startify'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'w0ng/vim-hybrid'
Plug 'preservim/tagbar'
Plug 'mattn/emmet-vim'
Plug 'skywind3000/asynctasks.vim'
Plug 'skywind3000/asyncrun.vim'
call plug#end()

" ===
" === Theme configuration
" ===
colorscheme	jellybeans
let g:jellybeans_overrides = {
\    'background': { 'guibg': '000000' },
\}

" ===
" === nerdtree configuration
" ===
nnoremap ,t :NERDTreeToggle<CR>
nnoremap ,f :NERDTreeFind<CR>
" Start NERDTree when Vim is started without file arguments.
"autocmd StdinReadPre * let s:std_in=1
"autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif
" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
" Exit Vim if NERDTree is the only window remaining in the only tab.
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
let NERDTreeShowHidden=1
let NERDTreeIgnore=['\.pyc','\~$','\.swp']

" ===
" === coc.nvim configuration
" ===
let g:coc_global_extensions = ['coc-json', 'coc-vimlsp', 'coc-css', 'coc-highlight', 'coc-html', 'coc-python', 'coc-tsserver', 'coc-marketplace']

set hidden
set encoding=utf-8
set updatetime=100
" Don't pass messages to |ins-completion-menu|.
set shortmess+=c

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved.
if has("nvim-0.5.0") || has("patch-8.1.1564")
  " Recently vim can merge signcolumn and number column into one
  set signcolumn=number
else
  set signcolumn=yes
endif
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction
inoremap <silent><expr> <c-space> coc#refresh()
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" Add missing imports on save
autocmd BufWritePre *.go :silent call CocAction('runCommand', 'editor.action.organizeImport')

" ===
" === tagbar configuration
" ===
nmap ,c :TagbarToggle<CR>

" ===
" === asyncrun.vim configuration
" ===
let g:asyncrun_open = 6
